<template>
  <div class="teaching-log">
    <el-table :data="tableData" height="50vh">
      <!-- <el-table-column prop="date" label="日期" width="120" /> -->
      <el-table-column prop="classIndex" label="课次" width="80" />
      <el-table-column prop="timeRange" label="时段" width="120" />
      <el-table-column prop="classHour" label="课时" width="100" />
      <el-table-column prop="curriculum" label="教学内容" />
      <el-table-column prop="teachingNewsNotes" label="教学动态记事">
        <template #default="scope">
          <el-input
            @blur="save(scope.row)"
            v-model="scope.row.teachingNewsNotes"
            placeholder="教学动态记事"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
let props = defineProps(["info", "type", "archiveBinding"]);

let tableData = ref([]);
let list = [];
let templateUrl;
let classData = ref({});

async function getInfo() {
  let { archiveBindingList } = await counter.getTeachingJournal(props.info._id);
  tableData.value = archiveBindingList;
  getNotes();
}

async function getNotes() {
  let resp = await proxy.$apis.scheduleRelevant({
    data: { classId: props.info._id },
    type: "teaching-news-notes-list",
  });
  tableData.value.forEach((element) => {
    let index = proxy.$utils.findIndexByField(
      resp,
      "archiveNoteId",
      element._id
    );
    if (index != -1) element.teachingNewsNotes = resp[index].teachingNewsNotes;
  });
}

async function save(e) {
  await proxy.$apis.scheduleRelevant({
    type: "teaching-news-notes-save",
    data: {
      archiveNoteId: e._id,
      teachingNewsNotes: e.teachingNewsNotes,
      classId: props.info._id,
    },
  });
  proxy.$utils.showToast("保存成功");
  getNotes();
}

async function exportWord() {
  if (!templateUrl) return proxy.$utils.showToast("请先设置模板");
  //return console.log("list", arr);
  let url = await proxy.$apis.classDocExport({
    templateURL: templateUrl,
    data: {
      list: await counter.teachingLogExportParams(
        tableData.value,
        list,
        props.info._id
      ),
    },
    fileName: "教学日志.docx",
  });
  window.location.href = url;
}

function downLoadTemplate() {
  window.location.href = templateUrl;
}

async function getTemplateUrl() {
  templateUrl = await proxy.$apis.fileTemplate({
    data: { type: props.type },
    type: "downLoad",
  });
}

watch(
  () => [props.info, props.archiveBinding],
  (newValue, oldValue) => {
    if (newValue) {
      getTemplateUrl();
      getInfo();
    }
  },
  { deep: true, immediate: true }
);

defineExpose({ exportWord, downLoadTemplate, getTemplateUrl });
</script>

<style lang="scss" scoped>
.teaching-log {
}
</style>
